package com.数组中出现次数超过一半的数字;

public class Solution {
	//1,2,3,2,4,2,5,2,3
	public int MoreThanHalfNum_Solution(int[] array) {
		if (array.length < 1)
			return 0;
		if (array.length == 1)
			return array[0];
		int result = array[0];
		int times = 1;
		for (int i = 1; i < array.length; i++) {
			//System.out.println("result="+result+"times="+times);
			if (times == 0) {
				result = array[i];
				times = 1;
			} else {
				if (array[i] == result)
					times++;
				else {
					times--;
				}
			}
		}
		times=0;
		for(int i=0;i<array.length;i++) {
			if(result==array[i])
				times++;
		}
		if(times>array.length/2)
			return result;
		else
			return 0;
	}
}
